package com.cedifie.lendapp.ui.user.adapter

import android.annotation.SuppressLint
import android.graphics.Color
import android.graphics.Typeface
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.cedifie.lendapp.R
import com.cedifie.lendapp.databinding.ItemPermissionReasonChildBinding
import com.cedifie.lib_baseadapter.CEDRecyclerViewAdapter
import com.cedifie.lib_baseadapter.CEDViewHolderHelper
import org.json.JSONObject

/**
 * Cedifie
 * @Author wwdeng
 * @CreateTime: 2024/4/24 14:42
 * @Annotation：权限声明列表 适配器
 */
class CedPermissionReasonAdapter(recyclerView: RecyclerView?) : CEDRecyclerViewAdapter<JSONObject>(recyclerView) {

    override fun getItemViewType(position: Int): Int {
        val model = getItem(position)
        if (model.optString("itemType") == "noticeItem"){
            return R.layout.item_permission_notice
        }
        return R.layout.item_permission_reason
    }

    @SuppressLint("SetTextI18n")
    override fun fillData(helper: CEDViewHolderHelper, position: Int, model: JSONObject) {
        if (model.optString("itemType") != "noticeItem"){
            helper.setText(R.id.text_title,model.optString("title"))

            val imgIcon = helper.getImageView(R.id.img_icon)
            imgIcon.setImageResource(model.optInt("icon"))

            val reasonLayout = helper.getView<LinearLayout>(R.id.reason_layout)
            reasonLayout.removeAllViews()

            val itemList = model.optJSONArray("msgList")
            itemList?.let {
                for (i in 0..< itemList.length()){
                    val item = itemList.optJSONObject(i)
                    val itemView = ItemPermissionReasonChildBinding.inflate(LayoutInflater.from(mContext))
                    val showTitle = item.optString("title")
                    if (showTitle.isNotEmpty()){
                        itemView.textTitle.text = showTitle
                        itemView.textTitle.visibility = View.VISIBLE
                    } else {
                        itemView.textTitle.visibility = View.GONE
                    }
                    itemView.textReason.text = item.optString("des")
                    reasonLayout.addView(itemView.root)
                }
            }
        }
    }
}